package com.spzx.alex008cache20250513.service;

import com.spzx.alex008cache20250513.annotation.Alex008Cache;
import com.spzx.alex008cache20250513.entity.User;
import com.spzx.alex008cache20250513.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;

import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
public class Alex008UserService {

    @Autowired
    private  UserMapper userMapper;

    @Alex008Cache(
        key = "'user:' + #id",
        expire = 30,
        timeUnit = TimeUnit.MINUTES,
        lock = true
    )
    public User getUser(Long id) {
        return userMapper.selectById(id);
    }
    
    @Alex008Cache(
        key = "'user:' + #user.id",
        deleteCache = true,
        async = true
    )
    public void updateUser(User user) {
        userMapper.updateById(user);
    }
}